<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['pendingReport'][$_SESSION['s_userType'.$cfgPack]])
      && $cfgFileNames['pendingReport'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
  $newInstallDetailArray   = array();
  $newInstallCount         = 0;
  $complainCallDetailArray = array();
  $complainCount           = 0;
  $otherServiceArray       = array();
  $otherCount              = 0;
  $msg                     = "";
  $complainServiceId       = isset($_GET['complainServiceId']) ? $_GET['complainServiceId'] : 0;
  $newInstallationMasterId = isset($_GET['newInstallationMasterId'])? $_GET['newInstallationMasterId'] : 0;
  $otherServiceId          = isset($_GET['otherServiceId']) ? $_GET['otherServiceId'] : 0;
  if($complainServiceId > 0)
  {
    $newInstallationMasterId = -1;
    $otherServiceId = -1;
  }
  if($newInstallationMasterId > 0)
  {
    $complainServiceId = -1;
    $otherServiceId = -1;
  }
  if($otherServiceId > 0)
  {
    $complainServiceId = -1;
    $newInstallationMasterId = -1;
  }
  
  if(isset($_GET['complainServiceId']) && $_GET['complainServiceId'] > 0)
  {
    $selectComplainEntry = "SELECT complainservice.complainServiceId,DATE_FORMAT(complainservice.complainDate,'%m-%d-%Y') AS complainDate,
                                 DATE_FORMAT(complainservice.targetedDate,'%m-%d-%Y') AS targetedDate,complainservice.description,
                                 complainservice.serviceCharge,complainservice.remarks,complainservice.amountCollected,
                                 party.partyId,party.companyName,party.mobile1,party.contactPerson1,
                                 category.categoryId,category.categoryName
                            FROM complainservice
                            JOIN party      ON party.partyId           = complainservice.partyId
                            JOIN category   ON category.categoryId     = complainservice.categoryId
                           WHERE complainservice.status = 'A'
                             AND complainServiceId =".$_REQUEST['complainServiceId'];
  }
  else
  {
    $selectComplainEntry = "SELECT complainservice.complainServiceId,DATE_FORMAT(complainservice.complainDate,'%m-%d-%Y') AS complainDate,
                                 DATE_FORMAT(complainservice.targetedDate,'%m-%d-%Y') AS targetedDate,complainservice.description,
                                 complainservice.serviceCharge,complainservice.remarks,complainservice.amountCollected,
                                 party.partyId,party.companyName,party.mobile1,party.contactPerson1,
                                 category.categoryId,category.categoryName
                            FROM complainservice
                            LEFT JOIN party      ON party.partyId           = complainservice.partyId
                            LEFT JOIN category   ON category.categoryId     = complainservice.categoryId
                           WHERE complainservice.status = 'A'";
  }
  $selectComplainEntryResult = mysql_query($selectComplainEntry);
  $complaneEntryRow = mysql_num_rows($selectComplainEntryResult);
  if($complaneEntryRow > 0)
  {
    while($complaneEntryRow = mysql_fetch_array($selectComplainEntryResult))
    {
      $complainCallDetailArray[$complainCount]['complainServiceId']  = $complaneEntryRow['complainServiceId'];
      $complainCallDetailArray[$complainCount]['companyName']        = $complaneEntryRow['companyName'];
      $complainCallDetailArray[$complainCount]['contactPerson1']     = $complaneEntryRow['contactPerson1'];
      $complainCallDetailArray[$complainCount]['mobile1']            = $complaneEntryRow['mobile1'];
      $complainCallDetailArray[$complainCount]['categoryName']       = $complaneEntryRow['categoryName'];
      $complainCallDetailArray[$complainCount]['complainDate']       = $complaneEntryRow['complainDate'];
      $complainCallDetailArray[$complainCount]['targetedDate']       = $complaneEntryRow['targetedDate'];
      $complainCallDetailArray[$complainCount]['description']        = $complaneEntryRow['description'];
      $complainCallDetailArray[$complainCount]['serviceCharge']      = $complaneEntryRow['serviceCharge'];
      $complainCallDetailArray[$complainCount]['remarks']            = $complaneEntryRow['remarks'];
      $complainCallDetailArray[$complainCount]['amountCollected']    = $complaneEntryRow['amountCollected'];
      $complainCount++;
   }
 }
 else
 {
  $msg = '<tr><td align="center" colspan="10"> <h2><font color="red"><b>Record Not Found...!</b></h2></font></td></tr>';
 }
  if(isset($_GET['newInstallationMasterId']) && $_GET['newInstallationMasterId'] > 0)
  {
    $selectNewInstall = "SELECT newinstallationmaster.newinstallationMasterId,newinstallationmaster.installationDate,
                                DATE_FORMAT(newinstallationmaster.installationDate,'%d-%m-%Y') AS installationDate,
                                newinstallationmaster.status,party.companyName,category.categoryName,
                                newinstallationdetail.description
                           FROM newinstallationmaster
                           LEFT JOIN newinstallationdetail ON newinstallationmaster.newinstallationMasterId = newinstallationdetail.newinstallationMasterId
                           LEFT JOIN party                 ON party.partyId             = newinstallationmaster.partyId
                           LEFT JOIN category              ON category.categoryId       = newinstallationdetail.categoryId
                          WHERE newinstallationmaster.status = 'A'
                            AND newinstallationmaster.newinstallationMasterId=".$_REQUEST['newInstallationMasterId'];
  }
  else
  {
    $selectNewInstall = "SELECT newinstallationmaster.newinstallationMasterId,newinstallationmaster.installationDate,
                                DATE_FORMAT(newinstallationmaster.installationDate,'%d-%m-%Y') AS installationDate,
                                newinstallationmaster.status,party.companyName,category.categoryName,
                                newinstallationdetail.description
                           FROM newinstallationmaster
                           LEFT JOIN newinstallationdetail ON newinstallationmaster.newinstallationMasterId = newinstallationdetail.newinstallationMasterId
                           LEFT JOIN party                 ON party.partyId             = newinstallationmaster.partyId
                           LEFT JOIN category              ON category.categoryId       = newinstallationdetail.categoryId
                          WHERE newinstallationmaster.status = 'A'";
  }
  $selectNewInstallResult = mysql_query($selectNewInstall);
  $newInstallRow = mysql_num_rows($selectNewInstallResult);
  if($newInstallRow > 0)
  {
    while($newInstallRow = mysql_fetch_array($selectNewInstallResult))
    {
      if(isset($newInstallDetailArray[$newInstallCount - 1]['newinstallationMasterId'])
         && $newInstallDetailArray[$newInstallCount - 1]['newinstallationMasterId']  == $newInstallRow['newinstallationMasterId'])// && date prev and current both same
      {
        $newInstallDetailArray[$newInstallCount]['display'] = 0;
      }
      else
      {
        $newInstallDetailArray[$newInstallCount]['display'] = 1;
      }

      $newInstallDetailArray[$newInstallCount]['newinstallationMasterId'] = $newInstallRow['newinstallationMasterId'];
      $newInstallDetailArray[$newInstallCount]['companyName']       = $newInstallRow['companyName'];
      $newInstallDetailArray[$newInstallCount]['categoryName']      = $newInstallRow['categoryName'];
      $newInstallDetailArray[$newInstallCount]['installationDate']  = $newInstallRow['installationDate'];
      $newInstallDetailArray[$newInstallCount]['description']       = $newInstallRow['description'];
      $newInstallCount++;
   }
  }
  else
  {
    $msg = '<tr><td align="center" colspan="10"> <h2><font color="red"><b>Record Not Found...!</b></h2></font></td></tr>';
  }
  if(isset($_GET['otherServiceId']) && $_GET['otherServiceId'] > 0)
  {
    $selectOtherService = "SELECT otherservice.otherServiceId,otherservice.partyId,otherservice.serviceType,
                                  DATE_FORMAT(otherservice.complainDate,'%d-%m-%Y') AS complainDate,
                                  DATE_FORMAT(otherservice.targetedDate,'%d-%m-%Y') AS targetedDate,
                                  otherservice.categoryId,otherservice.description,otherservice.importantPoint,
                                  otherservice.techAllocated,otherservice.status,
                                  party.companyName,party.partyId,category.categoryId,category.categoryName
                             FROM otherservice
                             LEFT JOIN party                 ON party.partyId             = otherservice.partyId
                             LEFT JOIN category              ON category.categoryId       = otherservice.categoryId
                            WHERE otherservice.status = 'A'
                              AND otherServiceId=".$_REQUEST['otherServiceId'];
  }
  else
  {
    $selectOtherService = "SELECT otherservice.otherServiceId,otherservice.partyId,otherservice.serviceType,
                                  DATE_FORMAT(otherservice.complainDate,'%d-%m-%Y') AS complainDate,
                                  DATE_FORMAT(otherservice.targetedDate,'%d-%m-%Y') AS targetedDate,
                                  otherservice.categoryId,otherservice.description,otherservice.importantPoint,
                                  otherservice.techAllocated,otherservice.status,
                                  party.companyName,party.partyId,category.categoryId,category.categoryName
                             FROM otherservice
                             LEFT JOIN party                 ON party.partyId             = otherservice.partyId
                             LEFT JOIN category              ON category.categoryId       = otherservice.categoryId
                            WHERE otherservice.status = 'A'";
  }
  $selectOtherServiceResult = mysql_query($selectOtherService);
  while($OtherServiceResultRow = mysql_fetch_array($selectOtherServiceResult))
  {
    $otherServiceArray[$otherCount]['otherServiceId']    = $OtherServiceResultRow['otherServiceId'];
    $otherServiceArray[$otherCount]['companyName']       = $OtherServiceResultRow['companyName'];
    $otherServiceArray[$otherCount]['serviceType']       = $OtherServiceResultRow['serviceType'];
    $otherServiceArray[$otherCount]['categoryName']      = $OtherServiceResultRow['categoryName'];
    $otherServiceArray[$otherCount]['complainDate']      = $OtherServiceResultRow['complainDate'];
    $otherServiceArray[$otherCount]['targetedDate']      = $OtherServiceResultRow['targetedDate'];
    $otherServiceArray[$otherCount]['description']       = $OtherServiceResultRow['description'];
    $otherServiceArray[$otherCount]['importantPoint']    = $OtherServiceResultRow['importantPoint'];
    $otherCount++;
  }
  if($otherCount == 0)
  {
    $msg = '<tr><td align="center" colspan="10"> <h2><font color="red"><b>Record Not Found...!</b></h2></font></td></tr>';
  }

  include("./bottom.php");
  $smarty->assign("msg",$msg);
  $smarty->assign("newInstallDetailArray",$newInstallDetailArray);
  $smarty->assign("complainCallDetailArray",$complainCallDetailArray);
  $smarty->assign("otherServiceArray",$otherServiceArray);
  $smarty->assign("complainServiceId",$complainServiceId);
  $smarty->assign("newInstallationMasterId",$newInstallationMasterId);
  $smarty->assign("otherServiceId",$otherServiceId);
  $smarty->display("pendingReport.tpl");
}
?>